package com.witmore.neutron.ai.cognitive.others;

import com.witmore.neutron.ai.api.dto.MessageDTO;
import com.witmore.neutron.ai.db.entity.ModelConfigDO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ModelRouterService {

    private final Map<ModelType, AiModelAdapter> modelAdapters;

    public ModelRouterService(List<AiModelAdapter> adapters) {
        this.modelAdapters = adapters.stream()
                .collect(Collectors.toMap(AiModelAdapter::getModelName, Function.identity()));
    }

    public String routeRequest(ModelConfigDO modelConfigDO, MessageDTO messageBTO) {
        // 模型类型
        String modelType = modelConfigDO.getModelType();
        // 转换成枚举
        ModelType modelName = ModelType.valueOf(modelType);
        AiModelAdapter adapter = modelAdapters.get(modelName);
        if (adapter == null) {
            throw new IllegalArgumentException("Unsupported model: " + modelName);
        }
        return adapter.generateResponse(modelConfigDO, messageBTO);
    }
}